/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32g4xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "math.h"
#include <stdbool.h>
#include "mymodbus.h"
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */
extern uint8_t debugval;
extern uint32_t debugnum;

#define usart1_buffersize 256
extern uint32_t system_time_s;
extern char usart1_recvBuff[usart1_buffersize];
extern char ModBus_recvBuff[8];
extern char ModBus_sendBuff[32];

extern volatile uint8_t recvLength;  //接收一帧数据的长度
extern volatile uint8_t recvEndFlag; //一帧数据接收完成标志

extern uint16_t modbusdebug;


extern uint16_t periodCount_500ms;
extern uint16_t periodCount_10ms;
extern uint8_t periodFLAG_50us;
extern uint8_t periodFLAG_10ms;
extern uint8_t periodFLAG_500ms;
extern uint16_t CurrentADC_Value;

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */
extern CRC_HandleTypeDef hcrc;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart3;
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim7;
extern DMA_HandleTypeDef hdma_tim1_com;
extern ADC_HandleTypeDef hadc1;
extern DMA_HandleTypeDef hdma_usart1_rx;
extern DMA_HandleTypeDef hdma_usart1_tx;
extern I2C_HandleTypeDef hi2c1;
/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define LED_Pin GPIO_PIN_13
#define LED_GPIO_Port GPIOC
#define BREAK_PVDD_Pin GPIO_PIN_14
#define BREAK_PVDD_GPIO_Port GPIOC
#define RS485_RE_Pin GPIO_PIN_0
#define RS485_RE_GPIO_Port GPIOB

/* USER CODE BEGIN Private defines */

#define Time_VARS_INIT					{0, 0, 0, 0, 0,0, 4000,20, 10000, 200, 20000}
#define MOTOR_VARS_INIT					{    0,         0,            0,         1, 0,0,0, 0,			0,  	   	 500,            100,              0,       80000}
//															运行状态  启动标志位   停止标志位				DIR  	    			控制方式	   目标占空比    实际占空比       实际速度      目标速度

#define FOOT_VARS_INIT					{    0,    0}
//																方向  	速度

#define HAND_VARS_INIT					{    0,    0,			0}
//																方向  	速度	温度
//#define CloseSpeedPI_VARS_INIT 	{0.1f, 0.01f, 0.0f, 3400.0f, 0.0f, 0.0f, 0, 0.00005f} // original 2400
//#define CloseSpeedPI_VARS_INIT 	{(2.5*1e-4), 0.00001f, 0.0f, 1.6f, 0.0f, 0.0f, 0, 0.00005f} // original 2400
#define CloseSpeedPI_VARS_INIT 	    {0.5f, 0.01f, 0.0f, 5665.0f, 100.0f, 0.0f, 500.0f, -500.0f, 0.0f, 0.00005f} // original 2400
#define CloseSpeed1PI_VARS_INIT 	{0.5f, 0.01f, 0.0f, 5665.0f, 100.0f, 0.0f, 500.0f, -500.0f, 0.0f, 0.00005f} // original 2400
#define ClosePositionPI_VARS_INIT 	{0.3f, 0.01f, 0.0f, 500.0f, -500.0f, 0.0f, 50.0f, -50.0f, 0.0f, 0.00005f} // original 2400
#define ClosePosition1PI_VARS_INIT 	{0.3f, 0.01f, 0.0f, 500.0f, -500.0f, 0.0f, 50.0f, -50.0f, 0.0f, 0.00005f} // original 2400

//#define CloseCurrentPI_VARS_INIT {2000.0f, 10.0f, 0.0f, 2400.0f, 0.0f, 0.0f, 0, 0.00005f}
//#define CloseCurrent1PI_VARS_INIT {2.0f, 0.08f, 0.0f, 2400.0f, 0.0f, 0.0f, 0, 0.00005f}


#define TIM1_DMA_BufferSize			6
#define MOTOR_IDLE							0
#define MOTOR_RUN								1
#define adc_max 4
#define FastStopSpeed						700						//0~5665

typedef struct
{
	bool periodFLAG_50us;
	bool periodFLAG_1ms;
	bool periodFLAG_10ms;
	bool periodFLAG_200ms;
	bool periodFLAG_500ms;
	bool periodFLAG_1s;
	uint16_t periodCount_200ms;
	uint16_t periodCount_1ms;
	uint16_t periodCount_500ms;
	uint16_t periodCount_10ms;
	uint16_t periodCount_1s;
} TimeFLAG_Typedef;

typedef struct
{
    float Kp;
    float Ki;
    float Kd;
    float Umax;              // 输出限幅
    float Umin;
    float I;                 // 积分项计算结果
    float Imax;
    float Imin;
    float last_Error;        // 上次误差
    double_t Ts;                // PID调节周期
} PID_Typedef;

typedef struct
{
	bool			motorStateFLAG;			//电机运行状态
	bool	 		motorStartFlAG;			//电机启动标志位
	bool			motorStopFlAG;			//电机停止标志位
  bool   		Motor_DIR;    			//0-顺时针，1-逆时针
	bool   		Motor_spinFlag;    	//电机转动标志位
	bool 			Motor_FastStopFlag;	//电机快减速标志位
	bool 			Motor_BrakeFlag;		//电机急停标志位
	bool 			Foot_ButtonErrorFlag;		//脚踏按键错误标志位
	uint8_t 	ControlMode;				//控制方式
  float_t  	Motor_AimDuty;   		//目标占空比
  float_t  	Motor_ActuralDuty;  //实际占空比
	float_t		Motor_ActuralSpeed;	//实际速度
	float_t		Motor_AimSpeed;			//目标速度
} MotorVars_t;

typedef struct
{
	bool			foot_dir;
	uint32_t 	foot_speed;
} Foot_Typedef;

typedef struct
{
	bool			hand_dir;
	uint32_t 	hand_speed;
	uint16_t 	hand_temprature;
} Hand_Typedef;

extern uint16_t My_adcData[adc_max];
extern bool LeftButton_state;
extern uint16_t TIM1_DMA_Buffer[TIM1_DMA_BufferSize];
extern volatile TimeFLAG_Typedef TimeVars;
extern volatile MotorVars_t Motor_Vars;
extern volatile PID_Typedef Speed_PI;
extern volatile PID_Typedef Speed1_PI;
extern volatile PID_Typedef Position_PI;
extern volatile PID_Typedef Position1_PI;
extern volatile PID_Typedef Current_PI;
extern volatile PID_Typedef Current1_PI;
extern volatile Foot_Typedef Foot_Vars;
extern volatile Hand_Typedef Hand_Vars;
extern bool reverse_pid_flag;
extern uint32_t test_count;
extern bool pretarget_finish_flag;
/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
